import javax.media.j3d.*;
import javax.vecmath.AxisAngle4f;
import javax.vecmath.Vector3f;

import java.util.*;

public abstract class HeadBehavior extends Behavior {

	protected TransformGroup viewPlatform;
    protected Transform3D transform_ = new Transform3D ();
    protected WakeupOr wakeupCriteria;
    
}


class TrackerHeadBehavior extends HeadBehavior {

    private Sensor sensor = null;
    private Transform3D transform_ = new Transform3D();

    
    public TrackerHeadBehavior(TransformGroup viewPlatform, Sensor sensor) {
    	this.viewPlatform = viewPlatform;
		this.sensor = sensor;
		wakeupCriteria = new WakeupOr(new WakeupCriterion[]{new WakeupOnElapsedFrames(0)});
    }
    
    public void initialize() {
    	wakeupOn(wakeupCriteria);
    }

    public void processStimulus (Enumeration criteria) {
    	
    	/* tracker control */
    	sensor.getRead(transform_);
    	viewPlatform.setTransform(transform_);

    	/*Next wake up condition*/
    	wakeupOn(wakeupCriteria);
    }
    
}


class KeyboardHeadBehavior extends HeadBehavior {

    private TransformGroup viewPlatform;
    private KeyboardDevice dev = null;
    
    
    public KeyboardHeadBehavior(TransformGroup viewPlatform, KeyboardDevice dev){
    	this.viewPlatform = viewPlatform;
    	this.dev = dev;
    	wakeupCriteria = new WakeupOr(new WakeupCriterion[]{new WakeupOnElapsedFrames(0)});
    }

    public void initialize() {
    	wakeupOn(wakeupCriteria);
    }

    public void processStimulus (Enumeration criteria) {

    	/* keyboard control */
		if (dev.getButton(11))
			rotateAboutVertical(0.025f);
		if (dev.getButton(12))
			rotateAboutVertical(-0.025f);

    	/*Next wake up condition*/
    	wakeupOn(wakeupCriteria);
    }
    
    protected void rotateAboutVertical(float radians) {

		Transform3D viewTransform = new Transform3D();
		viewPlatform.getTransform(viewTransform);
	
		Vector3f up = new Vector3f(0f, 1f, 0f);
		viewTransform.transform(up);
		AxisAngle4f rotation = new AxisAngle4f (up.x, up.y, up.z, radians);
		Transform3D rotationTform = new Transform3D();
		rotationTform.setRotation(rotation);
	
		viewTransform.mul(rotationTform);
		viewPlatform.setTransform(viewTransform);
    }
}
